package tools

import "encoding/json"

// GetSingleChoiceSchema 获取单选题JSON Schema
func GetSingleChoiceSchema() json.RawMessage {
	schema := map[string]interface{}{
		"type": "object",
		"properties": map[string]interface{}{
			"question": map[string]interface{}{
				"type":        "string",
				"description": "问题文本",
			},
			"options": map[string]interface{}{
				"type":        "array",
				"description": "选项列表",
				"items": map[string]interface{}{
					"type":      "string",
					"minLength": 1,
				},
				"minItems": 2,
			},
			"timeout": map[string]interface{}{
				"type":        "integer",
				"description": "超时时间（秒，0 表示不超时）",
				"default":     0,
				"minimum":     0,
				"maximum":     86400,
			},
		},
		"required": []string{"question", "options"},
	}
	data, _ := json.Marshal(schema)
	return data
}

// GetMultipleChoiceSchema 获取多选题JSON Schema
func GetMultipleChoiceSchema() json.RawMessage {
	schema := map[string]interface{}{
		"type": "object",
		"properties": map[string]interface{}{
			"question": map[string]interface{}{
				"type":        "string",
				"description": "问题文本",
			},
			"options": map[string]interface{}{
				"type":        "array",
				"description": "选项列表",
				"items": map[string]interface{}{
					"type":      "string",
					"minLength": 1,
				},
				"minItems": 2,
			},
			"timeout": map[string]interface{}{
				"type":        "integer",
				"description": "超时时间（秒，0 表示不超时）",
				"default":     0,
				"minimum":     0,
				"maximum":     86400,
			},
		},
		"required": []string{"question", "options"},
	}
	data, _ := json.Marshal(schema)
	return data
}

// GetFreeTextSchema 获取简答题JSON Schema
func GetFreeTextSchema() json.RawMessage {
	schema := map[string]interface{}{
		"type": "object",
		"properties": map[string]interface{}{
			"question": map[string]interface{}{
				"type":        "string",
				"description": "问题文本",
			},
			"placeholder": map[string]interface{}{
				"type":        "string",
				"description": "输入框占位符",
			},
			"timeout": map[string]interface{}{
				"type":        "integer",
				"description": "超时时间（秒，0 表示不超时）",
				"default":     0,
				"minimum":     0,
				"maximum":     86400,
			},
		},
		"required": []string{"question"},
	}
	data, _ := json.Marshal(schema)
	return data
}

// GetAskQuestionsSchema 获取批量提问JSON Schema
func GetAskQuestionsSchema() json.RawMessage {
	// 在批量模式下：
	// - 当 type 为 single 或 multiple 时，必须提供 options，且至少 2 项
	// - 允许缺省 id（由服务端在适配阶段自动补全），但从 Schema 角度仍推荐提供
	items := map[string]interface{}{
		"type": "object",
		"properties": map[string]interface{}{
			"id": map[string]interface{}{
				"type":        "string",
				"description": "问题唯一标识（未提供时将自动生成）",
			},
			"type": map[string]interface{}{
				"type":        "string",
				"description": "问题类型",
				"enum":        []string{"single", "multiple", "free_text"},
			},
			"question": map[string]interface{}{
				"type":        "string",
				"description": "问题文本",
			},
			"options": map[string]interface{}{
				"type":        "array",
				"description": "选项列表（单选/多选时必需）",
				"items": map[string]interface{}{
					"type":      "string",
					"minLength": 1,
				},
				"minItems": 2,
			},
			"placeholder": map[string]interface{}{
				"type":        "string",
				"description": "输入框占位符（简答题可选）",
			},
		},
		"required": []string{"type", "question"},
		"allOf": []interface{}{
			map[string]interface{}{
				"if": map[string]interface{}{
					"properties": map[string]interface{}{
						"type": map[string]interface{}{"const": "single"},
					},
				},
				"then": map[string]interface{}{
					"required": []string{"type", "question", "options"},
				},
			},
			map[string]interface{}{
				"if": map[string]interface{}{
					"properties": map[string]interface{}{
						"type": map[string]interface{}{"const": "multiple"},
					},
				},
				"then": map[string]interface{}{
					"required": []string{"type", "question", "options"},
				},
			},
		},
	}

	schema := map[string]interface{}{
		"type": "object",
		"properties": map[string]interface{}{
			"questions": map[string]interface{}{
				"type":        "array",
				"description": "问题列表",
				"items":       items,
				"minItems":    1,
			},
			"timeout": map[string]interface{}{
				"type":        "integer",
				"description": "整体超时时间（秒，0 表示不超时）",
				"default":     0,
				"minimum":     0,
				"maximum":     86400,
			},
		},
		"required": []string{"questions"},
	}
	data, _ := json.Marshal(schema)
	return data
}
